package com.cssy.yd.logic.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.yd.logic.dao.LogicMaterialReturnDAO;
import com.cssy.yd.logic.dao.LogicMaterialReturnItemBatchDAO;
import com.cssy.yd.logic.dao.LogicMaterialReturnItemDAO;
import com.cssy.yd.logic.entity.LogicMaterialReturnDO;
import com.cssy.yd.logic.entity.LogicMaterialReturnItemBatchDO;
import com.cssy.yd.logic.entity.LogicMaterialReturnItemDO;
import com.cssy.yd.logic.entity.LogicWhAffDO;
import com.cssy.yd.logic.service.ILogicMaterialReturnService;
import com.cssy.yd.logic.service.ILogicWhAffStockService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.exception.SyCodeException;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;
@Service("logicMaterialReturnService")
public class LogicMaterialReturnServiceImpl implements ILogicMaterialReturnService{

	@Resource(name = "logicMaterialReturnDAO")
	private LogicMaterialReturnDAO logicMaterialReturnDAO;
	@Resource(name = "logicMaterialReturnItemDAO")
	private LogicMaterialReturnItemDAO logicMaterialReturnItemDAO;
	
	@Resource(name = "logicWhAffStockService")
	private ILogicWhAffStockService logicWhAffStockService;
	
	@Resource(name="logicMaterialReturnItemBatchDAO")
	private LogicMaterialReturnItemBatchDAO logicMaterialReturnItemBatchDAO;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicMaterialReturnDO>  list= logicMaterialReturnDAO.find(scs);
		rs.setRecordCount(logicMaterialReturnDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		LogicMaterialReturnDO basic = new LogicMaterialReturnDO();
		basic.setDutime(new Date());
		basic.setDuuser(user.getName());
		basic.setId(id);
		basic.setRemoved(1);
		logicMaterialReturnItemDAO.deleteByMaReturnid(id);
		return logicMaterialReturnDAO.updateLogicMaterialReturnDO(basic);
	}

	@Override
	public Integer update(LogicMaterialReturnDO t, User user) throws SyBaseException {
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		return logicMaterialReturnDAO.updateLogicMaterialReturnDO(t);
	}

	@Override
	public LogicMaterialReturnDO save(LogicMaterialReturnDO t, User user)
			throws SyBaseException {
		Date date=new Date();
		t.setDcruser(user.getName());
		t.setDctime(date);
		logicMaterialReturnDAO.insertLogicMaterialReturnDO(t);
		return t;
	}

	@Override
	public LogicMaterialReturnDO findListById(String id) throws SyBaseException {
		return logicMaterialReturnDAO.findLogicMaterialReturnDOByPrimaryKey(id);
	}

	@Override
	public void tlSave(LogicMaterialReturnDO curAP, HttpServletRequest request)
			throws SyBaseException, SyCodeException {
		try{
			String tlDate = request.getParameter("tlDate");
			String json = request.getParameter("addressInfos");
			List<LogicMaterialReturnItemDO> axxlist=JSONArray.parseArray(json, LogicMaterialReturnItemDO.class);
			if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
				if (tlDate != null && !"".equals(tlDate)) {
					curAP.setTlDate(DateUtils.stringToDate(tlDate));
				}
				int rt=0;
				rt=this.update(curAP, SessionUtil.getUser(request));
				if(0==rt){
					throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
				}
				if(curAP.getBillStatus() != 3){
					List<LogicMaterialReturnItemDO> lDos= logicMaterialReturnItemDAO.findByMaReturnid(curAP.getId());
					if(lDos != null &&  lDos.size() > 0){
						for (int i = 0; i < lDos.size(); i++) {
							logicMaterialReturnItemBatchDAO.deleteBymareturnItemid(lDos.get(i).getId());
						}
						logicMaterialReturnItemDAO.deleteByMaReturnid(curAP.getId());
					}
					for (LogicMaterialReturnItemDO lmri : axxlist){
						String id = IDCodeTypeUtil.getId();
						lmri.setId(id);
						lmri.setMareturnId(curAP.getId());
						lmri.setMareturnCode(curAP.getMareturnCode());
						logicMaterialReturnItemDAO.insertLogicMaterialReturnItemDO(lmri);
						if(lmri.getItemBatch() != null && !"".equals(lmri.getItemBatch())){
							List<LogicMaterialReturnItemBatchDO> itemBatchDOs=JSONArray.parseArray(lmri.getItemBatch(), LogicMaterialReturnItemBatchDO.class);
							if(itemBatchDOs != null && itemBatchDOs.size() > 0){
								for (int i = 0; i < itemBatchDOs.size(); i++) {
									itemBatchDOs.get(i).setId(IDCodeTypeUtil.getId());
									itemBatchDOs.get(i).setMareturnitemid(id);
									logicMaterialReturnItemBatchDAO.insertLogicMaterialReturnItemBatchDO(itemBatchDOs.get(i));
								}
							}
						}
					}
				}
				if (curAP.getBillStatus() == 3){
					LogicMaterialReturnDO lmr = logicMaterialReturnDAO.findLogicMaterialReturnDOByPrimaryKey(curAP.getId());
					for (LogicMaterialReturnItemDO lmri : axxlist){
						if (curAP.getBillStatus() == 3){
							if("Y".equals(lmri.getOpenBatch())){
								if(lmri.getItemBatch() != null && !"".equals(lmri.getItemBatch())){
									List<LogicMaterialReturnItemBatchDO> itemBatchDOs=JSONArray.parseArray(lmri.getItemBatch(), LogicMaterialReturnItemBatchDO.class);
	//							if(lmri.getId() != null && !"".equals(lmri.getId())){
	//								List<LogicMaterialReturnItemBatchDO> itemBatchDOs=logicMaterialReturnItemBatchDAO.findbymareturnItemid(lmri.getId());
											if(itemBatchDOs != null && itemBatchDOs.size() > 0){
											for (int i = 0; i < itemBatchDOs.size(); i++) {
											LogicWhAffDO lw = new LogicWhAffDO();
											lw.setBillCode(lmr.getMareturnCode());
											lw.setBatch(itemBatchDOs.get(i).getBatch());
											lw.setBillType("TL");
											lw.setCode(lmri.getCode());
											lw.setDcruser(SessionUtil.getUser(request).getName());
											lw.setDctime(new Date());
											lw.setName(lmri.getName());
											lw.setQuantity(itemBatchDOs.get(i).getQuantity());
											lw.setSpec(lmri.getSpec());
											lw.setStatus(itemBatchDOs.get(i).getStatus());
											lw.setBrand(lmri.getBrand());
											lw.setHubName(curAP.getWhName());
											lw.setWhCode(lmr.getWhCode());
											lw.setWhname(lmr.getWhName());
											lw.setUnit(lmri.getUnit());
											lw.setJunit(lmri.getJunit());
											lw.setJquantity(itemBatchDOs.get(i).getJquantity());
											logicWhAffStockService.updateWhAffstockQuantity(lw, SessionUtil.getUser(request),false);
											}
									}
								}
							}else{
								LogicWhAffDO lw = new LogicWhAffDO();
								lw.setBillCode(lmr.getMareturnCode());
								lw.setBatch("NoBatch");
								lw.setBillType("TL");
								lw.setCode(lmri.getCode());
								lw.setDcruser(SessionUtil.getUser(request).getName());
								lw.setDctime(new Date());
								lw.setName(lmri.getName());
								lw.setQuantity(lmri.getQuantity());
								lw.setSpec(lmri.getSpec());
								lw.setStatus(lmri.getStatus());
								lw.setBrand(lmri.getBrand());
								lw.setHubName(curAP.getWhName());
								lw.setWhCode(lmr.getWhCode());
								lw.setWhname(lmr.getWhName());
								lw.setUnit(lmri.getUnit());
								lw.setJunit(lmri.getJunit());
								lw.setJquantity(lmri.getJquantity());
								logicWhAffStockService.updateWhAffstockQuantity(lw, SessionUtil.getUser(request),false);
							}
						}
					}
				}
			}else{//新增
					String mareturnCode=syCodeService.getCode("TL","TL",SyCode.DATE_YYMM,5);
					curAP.setMareturnCode(mareturnCode);
					String id = IDCodeTypeUtil.getId();
					curAP.setId(id);
					if (tlDate != null && !"".equals(tlDate)) {
						curAP.setTlDate(DateUtils.stringToDate(tlDate));
					}
					this.save(curAP, SessionUtil.getUser(request));
					for (LogicMaterialReturnItemDO lii : axxlist){
						String mid = IDCodeTypeUtil.getId();
						lii.setId(mid);
						lii.setMareturnId(id);
						logicMaterialReturnItemDAO.insertLogicMaterialReturnItemDO(lii);
						if(lii.getItemBatch() != null && !"".equals(lii.getItemBatch())){
							List<LogicMaterialReturnItemBatchDO> itemBatchDOs=JSONArray.parseArray(lii.getItemBatch(), LogicMaterialReturnItemBatchDO.class);
							if(itemBatchDOs != null && itemBatchDOs.size() > 0){
								for (int i = 0; i < itemBatchDOs.size(); i++) {
									itemBatchDOs.get(i).setId(IDCodeTypeUtil.getId());
									itemBatchDOs.get(i).setMareturnitemid(mid);
									logicMaterialReturnItemBatchDAO.insertLogicMaterialReturnItemBatchDO(itemBatchDOs.get(i));
								}
							}
//							lii.setItemBatch(JSONArray.toJSONString(itemBatchDOs));
//							logicMaterialReturnItemDAO.updateLogicMaterialReturnItemDO(lii);
						}
					}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}
}
